package com.unipay.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unipay.common.result.Result;
import com.unipay.common.utils.AuthContextHolder;
import com.unipay.enums.OrderStatusEnum;
import com.unipay.model.order.OrderInfo;
import com.unipay.order.service.OrderService;
import com.unipay.vo.order.OrderCountQueryVo;
import com.unipay.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Project: yitong_parent
 * @Author: Chenmy
 * @Time: 2023-07-04 20:56:11
 * @Desc: TODO
 *      下单参数：就诊人ID、排班ID
 *      1、获取就诊人信息
 *      2、获取“排班下单信息、规则信息”
 *      3、获取：医院签名信息、然后通过医院接口，去医院预约下单
 *      4、下单成功：更新排班信息，并发送短信
 */
@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "提交订单接口")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(
            @ApiParam(name = "scheduleId", value = "排班ID", required = true)
            @PathVariable String scheduleId,
            @ApiParam(name = "patientId", value = "排班ID", required = true)
            @PathVariable Long patientId,
            HttpServletRequest request
    ){
        Long orderId = orderService.saveOrder(scheduleId, patientId, request);
        return Result.ok(orderId);
    }

    @ApiOperation(value = "订单列表")
    @GetMapping("auth/{page}/{limit}")
    public Result listOrder(
            @ApiParam(name = "page", value = "页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "条数", required = true)
            @PathVariable Long limit,
            OrderQueryVo orderQueryVo,
            HttpServletRequest request
    ){
        // TODO 设置：当前用户ID，封装到---“查询 Vo”
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> orderInfoPage = new Page<>();
        IPage<OrderInfo> pageModel = orderService.selectPage(orderInfoPage, orderQueryVo);
        return Result.ok(pageModel);
    }
    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList(){
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "根据订单ID查询订单详情")
    @GetMapping("auth/getOrderById/{orderId}")
    public Result getOrderById(
            @ApiParam(name = "orderId", value = "订单ID", required = true)
            @PathVariable Long orderId
    ){
        OrderInfo orderInfo = orderService.getOrderById(orderId);
        return Result.ok(orderInfo);
    }

    /*
        取消订单分两种情况：
        1、未支付取消订单，直接通知医院更新取消预约状态
        2、已支付取消订单，先退款给用户，然后通知医院更新取消预约状态
            （1）根据订单id，得到订单信息
            （2）判断时间，是否超出取消时间
            （3）调用医院接口，进行取消
            （4）根据医院接口返回数据，进行如下操作：
                    更新订单状态
                    调用微信退款方法：根据支付记录，更新退款记录；第三方微信接口，进行退款。
     */
    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(
            @ApiParam(name = "orderId", value = "订单号", required = true)
            @PathVariable("orderId") Long orderId
    ){
        Boolean isCancel = orderService.cancelOrder(orderId);
        return Result.ok(isCancel);
    }

    // 统计订单
    @ApiOperation(value = "获取：订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(
            @RequestBody OrderCountQueryVo orderCountQueryVo
    ){
        Map<String, Object> resMap = orderService.getCountMap(orderCountQueryVo);
        return resMap;
    }
}
